package com.dudsbury.database;

import com.dudsbury.gui.CreateItemScreen;
import com.dudsbury.gui.SearchScreen;


public class MyItem implements Searchable,Cloneable {

	public boolean showQuantity = false;
	public int quantity =0 ;
	
	private String itemID;
	private String itemName;
	private String itemDescription;
	private int itemType;
	private float price;
	
	//Giving parameters constants
	private static final int ITEM_ID = 0;
	private static final int ITEM_NAME = 1;
	private static final int ITEM_DESCRIPTION = 2;
	private static final int ITEM_TYPE = 3;
	private static final int ITEM_PRICE= 4;
	
	String data[];
	//Default constructor method
	public MyItem(){
		
	}
	
	//Constructor method with an array
	public MyItem(String[] data){
		
		this.data=data;
		
		this.itemID=data[ITEM_ID];
		this.itemName=data[ITEM_NAME];
		this.itemDescription=data[ITEM_DESCRIPTION];
		this.itemType=Integer.valueOf(data[ITEM_TYPE]);
		this.price=Float.valueOf(data[ITEM_PRICE]);
	
	}

	public String getItemID() {
		return itemID;
	}

	public void setItemID(String itemID) {
		this.itemID = itemID;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getItemDescription() {
		return itemDescription;
	}

	public void setItemDescription(String itemDescription) {
		this.itemDescription = itemDescription;
	}

	public int getItemType() {
		return itemType;
	}

	public void setItemType(int itemType) {
		this.itemType = itemType;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}
	
	public String toString() {
		
		if (showQuantity) {
			return String.format("%s Quantity: %d", itemName,quantity);
		}
		else return itemName;
	}

	@Override
	public void onSelected(SearchScreen screen) {
		new CreateItemScreen(this,screen);
		
	}

	@Override
	public boolean search(String[] query) {

		for (String s : query) {
			
					if (itemName.equals(s) || itemDescription.equals(s)) {
						System.out.println("Found Result in Item");
						return true;
					}
				}
		
		return false;
	}

	@Override
	public String getReport() {
		return String.format("<html> <h1>Report for Item: %s</h1> \n Description: %s \n" +
				"Price : %.2f </html>", itemName,itemDescription,price);
	}
	
	
	public MyItem clone() {
		
		String[] dat = new String[5];
		
		dat[ITEM_ID]=itemID;
		dat[ITEM_NAME]=itemName;
		dat[ITEM_DESCRIPTION]=itemDescription;
		dat[ITEM_TYPE] = String.valueOf(itemType);
		dat[ITEM_PRICE] = String.valueOf(price);
		
		return new MyItem(dat);
	}

	
}
